﻿using System.Data.EntityClient;
using System.Data.SqlClient;

namespace MsSqlMetaDataProvider
{
    /// <summary>
    /// The factory class that returns Model object.
    /// </summary>
    public class ConnectionFactory
    {
        const string Edmx = "Model";
        /// <summary>
        /// Gets the connection string.
        /// </summary>
        /// <param name="serverName">Name of the server.</param>
        /// <param name="databaseName">Name of the database.</param>
        /// <param name="user">The user.</param>
        /// <param name="password">The password.</param>
        /// <returns></returns>
        public static string GetSqlConnectionString(string serverName, string databaseName, string user, string password)
        {
            var provider = new SqlConnectionStringBuilder
            {
                DataSource = serverName,
                InitialCatalog = databaseName,
                IntegratedSecurity = false,
                UserID = user,
                Password = password,
                ApplicationName = "MetaDataProvider"
            };

            return GetEntityConnectionString(provider.ToString());
        }
        /// <summary>
        /// Gets the connection string.
        /// </summary>
        /// <param name="serverName">Name of the server.</param>
        /// <param name="databaseName">Name of the database.</param>
        /// <returns></returns>
        public static string GetSqlConnectionString(string serverName, string databaseName)
        {
            var provider = new SqlConnectionStringBuilder
            {
                DataSource = serverName,
                InitialCatalog = databaseName,
                IntegratedSecurity = true,
                ApplicationName = "MetaDataProvider"
            };

            return GetEntityConnectionString(provider.ToString());
        }

        private static string GetEntityConnectionString(string provider)
        {
            var fullName = typeof(ConnectionFactory).Assembly.FullName;
            var csBuilder = new EntityConnectionStringBuilder
            {
                Provider = "System.Data.SqlClient",
                ProviderConnectionString = provider,
                Metadata =
                    string.Format("res://{0}/{1}.csdl|res://{0}/{1}.ssdl|res://{0}/{1}.msl", fullName, Edmx)
            };
            return csBuilder.ToString();
        }

        /// <summary>
        /// Gets the Model entity container.
        /// </summary>
        /// <param name="serverName">Name of the server.</param>
        /// <param name="databaseName">Name of the database.</param>
        /// <returns></returns>
        public static ModelContainer GetContainer(string serverName, string databaseName)
        {
            var conn = GetSqlConnectionString(serverName, databaseName);
            return new ModelContainer(conn);
        }
        /// <summary>
        /// Gets the Model entity container.
        /// </summary>
        /// <param name="serverName">Name of the server.</param>
        /// <param name="databaseName">Name of the database.</param>
        /// <param name="user">The user.</param>
        /// <param name="password">The password.</param>
        /// <returns></returns>
        public static ModelContainer GetContainer(string serverName, string databaseName, string user, string password)
        {
            var conn = GetSqlConnectionString(serverName, databaseName, user, password);
            return new ModelContainer(conn);
        }
        /// <summary>
        /// Gets the Model entity container.
        /// </summary>
        /// <param name="sqlConnectionString">The SQL connection string.</param>
        /// <returns></returns>
        public static ModelContainer GetContainer(string sqlConnectionString)
        {
            var conn = GetEntityConnectionString(sqlConnectionString);
            return new ModelContainer(conn);
        }
    }
}